package com.example.demo2;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/add")
public class add extends HttpServlet{
    private String message;
    public void init() throws ServletException
    {
        // 执行必需的初始化
        message = "add 添加成功";
    }
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        // 设置响应内容类型
//        response.setContentType("text/html;charset=UTF-8");
//        // 实际的逻辑是在这里
//        PrintWriter out = response.getWriter();
//        out.println("<h1>" + message + "</h1>");

        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("message","he2323");
//        内部跳转
//        request.getRequestDispatcher("hello-servlet").forward(request,response);
//        外部跳转
        response.sendRedirect("hello-servlet");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("text/html;charset=UTF-8");
        // 实际的逻辑是在这里
//        PrintWriter out = resp.getWriter();
//        out.println("<h1> post请求 </h1>");
        req.setAttribute("message","he2323");
        resp.sendRedirect("hello-servlet");

    }

    public void destroy()
    {
        // 什么也不做
    }
}
